const jwt = require('jsonwebtoken');
const longTerm = "24h"
const shortTerm = "1h"
// 生成短token
function getshortTerm(phone) {
    return "Bearer " + jwt.sign({ phone }, "short", { expiresIn: shortTerm })
}
// 生成短token
function getlongTerm(phone) {
    return "Bearer " + jwt.sign({ phone }, "long", { expiresIn: longTerm })
}

// 验证短token是否有效
function shortVerify(shortToken) {
    jwt.verify(shortToken, "lce", (err, data) => {
        if (err) { // 验证失败
            if (err & err.message == 'jwt expired') return console.log("token已过期");
            else if (err & err.message == 'invalid token') return console.log("token无效");
        } else { // 验证成功
            return true;
        }
    })
}

// 验证长token是否有效
function longVerify(longToken) {
    jwt.verify(longToken, "lce", (err, data) => {
        if (err) {
            if (err & err.message == 'jwt expired') return console.log("token已过期");
            else if (err & err.message == 'invalid token') return console.log("token无效");
        } else {
            return{
                longToken:getlongTerm(),
                shortTken:getshortTerm()
            }
        }
    })
}

module.exports = {
    getshortTerm,
    getlongTerm,
    shortVerify,
    longVerify
}